package jiyun.com.yiyanmeng;

import android.content.Intent;
import android.support.design.widget.TabItem;
import android.support.design.widget.TabLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import base.BaseActivity;
import bean.LoginBean;
import presenter.LoginPresenter;
import utils.LogUtil;
import utils.SpUtil;
import utils.ToastUtil;
import view.LoginView;

public class LoginActivity extends BaseActivity<LoginView, LoginPresenter> implements LoginView, View.OnClickListener {


    private ImageView mIvBack;
    /**
     * 账号密码登录
     */
    private TabItem mTabitemPwdlogin;
    /**
     * 手机号快速登录
     */
    private TabItem mTabitemPhonelogin;
    private TabLayout mTabLogin;
    /**
     * +86
     */
    private TextView mTxt1;
    private View mView1;
    /**
     * 请输入手机号
     */
    private EditText mEtQuickPhone;
    /**
     * 请输入验证码
     */
    private EditText mEtQuickCode;
    private View mView2;
    /**
     * 获取验证码
     */
    private TextView mTvQuickGetCode;
    /**
     * 登录
     */
    private Button mBtSubmitQuickLogin;
    /**
     * 用户注册
     */
    private TextView mTvQuickRegister;
    private LinearLayout mLayoutQuick;
    /**
     * 请输入用户名/手机号
     */
    private EditText mEtPhone;
    /**
     * 请输入密码
     */
    private EditText mEtPass;
    /**
     * 登录
     */
    private Button mBtLogin;
    /**
     * 忘记密码
     */
    private TextView mTvMisspass;
    private View mView;
    /**
     * 用户注册
     */
    private TextView mTvRegister;
    private LinearLayout mLayoutPass;
    private String mEtphone;
    private String mEtpass;

    @Override
    public void showToast(String msg) {
        ToastUtil.show(this,msg);
    }

    @Override
    protected void initData() {
       // mPresenter.requestLogin(mEtphone,mEtpass);
    }

    @Override
    protected void setListener() {

        mTabLogin.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                switch (position) {
                    case 0:
                        mLayoutPass.setVisibility(View.VISIBLE);
                        mLayoutQuick.setVisibility(View.GONE);
                        break;
                    case 1:
                        mLayoutPass.setVisibility(View.GONE);
                        mLayoutQuick.setVisibility(View.VISIBLE);
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    protected void initView() {

        mIvBack = (ImageView) findViewById(R.id.iv_back);
        mIvBack.setOnClickListener(this);
        mTabitemPwdlogin = (TabItem) findViewById(R.id.tabitem_pwdlogin);
        mTabitemPhonelogin = (TabItem) findViewById(R.id.tabitem_phonelogin);
        mTabLogin = (TabLayout) findViewById(R.id.tab_login);
        mTxt1 = (TextView) findViewById(R.id.txt1);
        mView1 = (View) findViewById(R.id.view1);
        mEtQuickPhone = (EditText) findViewById(R.id.et_quick_phone);
        mEtQuickCode = (EditText) findViewById(R.id.et_quick_code);
        mView2 = (View) findViewById(R.id.view2);
        mTvQuickGetCode = (TextView) findViewById(R.id.tv_quick_get_code);
        mBtSubmitQuickLogin = (Button) findViewById(R.id.bt_submit_phone_login);
        mBtSubmitQuickLogin.setOnClickListener(this);
        mTvQuickRegister = (TextView) findViewById(R.id.tv_quick_register);
        mTvQuickRegister.setOnClickListener(this);
        mLayoutQuick = (LinearLayout) findViewById(R.id.layout_quick);
        mEtPhone = (EditText) findViewById(R.id.et_phone);
        mEtPass = (EditText) findViewById(R.id.et_pass);
        mBtLogin = (Button) findViewById(R.id.bt_submit_pass_login);
        mBtLogin.setOnClickListener(this);
        mTvMisspass = (TextView) findViewById(R.id.tv_misspass);
        mTvMisspass.setOnClickListener(this);
        mView = (View) findViewById(R.id.view);
        mTvRegister = (TextView) findViewById(R.id.tv_register);
        mTvRegister.setOnClickListener(this);
        mLayoutPass = (LinearLayout) findViewById(R.id.layout_pass);
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected int bindLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_back:
                startActivity(new Intent(this, ShouYeActivity.class));
                finish();
                break;
            case R.id.bt_submit_phone_login:

                break;
            case R.id.bt_submit_pass_login:
                mEtphone = mEtPhone.getText().toString().trim();
                mEtpass = mEtPass.getText().toString().trim();
                if(mEtpass.isEmpty()|| mEtphone.isEmpty()){
                    showToast("账号密码均不能为空！");
                }else{
                    mPresenter.requestLogin(mEtphone,mEtpass);
                }
                break;
            case R.id.tv_register:
                startActivity(new Intent(this, RegActivity.class));
                break;
            case R.id.tv_misspass:
                startActivity(new Intent(this, MissPassActivity.class));
                break;
            case R.id.tv_quick_register:
                startActivity(new Intent(this, RegActivity.class));
                break;
        }
    }

    @Override
    public void onSuccess(LoginBean data) {
        showToast("登录成功");
        LoginBean.UserInfoBean user_info = data.getUser_info();
        String id = user_info.getId();
        String pass = user_info.getPass();
        String u_name = user_info.getU_name();
        String u_pic = user_info.getU_pic();
        String phnoe = user_info.getPhnoe();
        Object sex = user_info.getSex();
        String yu_bao_ming = user_info.getYu_bao_ming();
        String xinxi = user_info.getXinxi();

        LoginBean.TokenBean token = data.getToken();
        String access_token = token.getAccess_token();
        String expires = token.getExpires();
        String refreshTokenExpires = token.getRefreshTokenExpires();
        String refresh_token = token.getRefresh_token();
        //存入SP
        SpUtil.put(LoginActivity.this, "phone", phnoe);
        SpUtil.put(LoginActivity.this, "isLogin", true);
        SpUtil.put(LoginActivity.this, "u_name", u_name);
        SpUtil.put(LoginActivity.this, "u_pic", u_pic);
        SpUtil.put(LoginActivity.this, "access_token", access_token);
        SpUtil.put(LoginActivity.this, "expires", expires);
        SpUtil.put(LoginActivity.this, "refresh_token",refresh_token);
        SpUtil.put(LoginActivity.this,"pass",pass);
        if(TextUtils.isEmpty(yu_bao_ming)){
            //没有填写信息，则跳转页面补充信息
            startActivity(new Intent(LoginActivity.this,EditInfoActivity.class));
        }else{
            //补充过信息，则跳转首页题库页面
            startActivity(new Intent(LoginActivity.this,ShouYeActivity.class));
        }

        //登录成功跳转到首页
        startActivity(new Intent(this, ShouYeActivity.class));
        finish();
    }

    @Override
    public void onFail(String error) {
        showToast("账号或密码不正确，请重新登录！");
    }
}
